﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using System.IO;


namespace functionService // service function namespace
{
    
    class shorts // short  service function
    {
        public static string read(string so, int ii)
        {
            int il = so.Length;
            string s = so.Substring(il - ii, ii); return s;
        }
        public static int TRUFAL(bool btrueFalse)
        {
            int i = 0;
            if (btrueFalse) { i = 1; } else { i = 0; }
            return i;
        }
        #region n nn nnn
        public static string n(object o)
        {
            string s = "";
            if (o.ToString() != "0") { s = o.ToString(); }
            return s;
        }
        public static string nn(object o)
        {
            string s = "0"; int io = Convert.ToInt32(o);
            if (io < 10) { s = string.Concat("0", io); } else { s = io.ToString(); }
            return s;
        }
        public static string nnn(object o)
        {
            string s = "0", ss = ""; int io = Convert.ToInt32(o), ioo = 0;
            if (io < 1000) { ioo = 1000 + io; s = ioo.ToString(); ss = s.Substring(1, 3); } else { ss = "001"; }
            return ss;
        }
        public static string fo(object o)
        {
            return String.Format("{0:0.00}", o);//String.Format("{0:0.00}", 123.4567)
        }
        
        #endregion
        #region i m


        public static int i(object o)
        {
            return Convert.ToInt32(o);
        }
        public static void m(object o)
        {
            System.Windows.Forms.MessageBox.Show(o.ToString());
        }
        public static void m(object o, object o1)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1));
        }
        public static void m(object o, object o1, object o2)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1, " ", o2));
        }
        public static void m(object o, object o1, object o2, object o3)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1, " ", o2, " ", o3));
        }
        public static void m(object o, object o1, object o2, object o3, object o4)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1, " ", o2, " ", o3, " ", o4));
        }
        public static void m(object o, object o1, object o2, object o3, object o4, object o5)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1, " ", o2, " ", o3, " ", o4, " ", o5));
        }
        public static void m(object o, object o1, object o2, object o3, object o4, object o5, object o6)
        {
            System.Windows.Forms.MessageBox.Show(string.Concat(o, " ", o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6));
        }
        #endregion
        #region a  aa
        public static string a(object o, object oo)
        {
            return string.Concat(o, oo);
        }
        public static string a(object o, object oo, object ooo)
        {
            return string.Concat(o, oo, ooo);
        }
        public static string a(object o, object oo, object ooo, object oooo)
        {
            return string.Concat(o, oo, ooo, oooo);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5)
        {
            return string.Concat(o1, o2, o3, o4, o5);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7, o8);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7, o8, o9);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10, object o11)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11);
        }
        public static string a(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10, object o11, object o12)
        {
            return string.Concat(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12);
        }




        public static string az(object o, object ooo)
        {
            return string.Concat(o, ";", ooo);
        }
        public static string az(object o, object oo, object ooo)
        {
            return string.Concat(o, ";", oo, ";", ooo);
        }
        public static string az(object o, object oo, object ooo, object oooo)
        {
            return string.Concat(o, ";", oo, ";", ooo, ";", oooo);
        }
        public static string az(object o1, object o2, object o3, object o4, object o5)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5);
        }
        public static string az(object o1, object o2, object o3, object o4, object o5, object o6)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5, ";", o6);
        }
        public static string az(object o1, object o2, object o3, object o4, object o5, object o6, object o7)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5, ";", o6, ";", o7, " ");
        }
        public static string az(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5, ";", o6, ";", o7, ";", o8, ";", " ");
        }
        public static string az(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5, ";", o6, ";", o7, ";", o8, ";", o9, " ");
        }
        public static string az(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10)
        {
            return string.Concat(o1, ";", o2, ";", o3, ";", o4, ";", o5, ";", o6, ";", o7, ";", o8, ";", o9, ";", o10);
        }












        public static string aa(object o, object ooo)
        {
            return string.Concat(o, " ", ooo);
        }
        public static string aa(object o, object oo, object ooo)
        {
            return string.Concat(o, " ", oo, " ", ooo);
        }
        public static string aa(object o, object oo, object ooo, object oooo)
        {
            return string.Concat(o, " ", oo, " ", ooo, " ", oooo);
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5);
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6);
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ");
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ", o8, " ", " ");
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ", o8, " ", o9, " ");
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ", o8, " ", o9, " ", o10);
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10, object o11)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ", o8, " ", o9, " ", o10, " ", o11);
        }
        public static string aa(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8, object o9, object o10, object o11, object o12)
        {
            return string.Concat(o1, " ", o2, " ", o3, " ", o4, " ", o5, " ", o6, " ", o7, " ", o8, " ", o9, " ", o10, " ", o11, " ", o12);
        }
        public static string aa(
            object o1,
            object o2,
            object o3,
            object o4,
            object o5,
            object o6,
            object o7,
            object o8,
            object o9,
            object o10,
            object o11,
            object o12,
            object o13)
        {
            return string.Concat(
                o1, " ",
                o2, " ",
                o3, " ",
                o4, " ",
                o5, " ",
                o6, " ",
                o7, " ",
                o8, " ",
                o9, " ",
                o10, " ",
                o11, " ",
                o12, " ",
                o13);
        }
        public static string aa(
            object o1,
            object o2,
            object o3,
            object o4,
            object o5,
            object o6,
            object o7,
            object o8,
            object o9,
            object o10,
            object o11,
            object o12,
            object o13,
            object o14)
        {
            return string.Concat(
                o1, " ",
                o2, " ",
                o3, " ",
                o4, " ",
                o5, " ",
                o6, " ",
                o7, " ",
                o8, " ",
                o9, " ",
                o10, " ",
                o11, " ",
                o12, " ",
                o13, " ",
                o14);
        }
        public static string aa(
           object o1,
           object o2,
           object o3,
           object o4,
           object o5,
           object o6,
           object o7,
           object o8,
           object o9,
           object o10,
           object o11,
           object o12,
           object o13,
           object o14,
           object o15)
        {
            return string.Concat(
                o1, " ",
                o2, " ",
                o3, " ",
                o4, " ",
                o5, " ",
                o6, " ",
                o7, " ",
                o8, " ",
                o9, " ",
                o10, " ",
                o11, " ",
                o12, " ",
                o13, " ",
                o14, " ",
                o15);
        }

        public static string aa(
           object o1,
           object o2,
           object o3,
           object o4,
           object o5,
           object o6,
           object o7,
           object o8,
           object o9,
           object o10,
           object o11,
           object o12,
           object o13,
           object o14,
            object o15,
           object o16)
        {
            return string.Concat(
                o1, " ",
                o2, " ",
                o3, " ",
                o4, " ",
                o5, " ",
                o6, " ",
                o7, " ",
                o8, " ",
                o9, " ",
                o10, " ",
                o11, " ",
                o12, " ",
                o13, " ",
                o14, " ",
                o15, " ",
                o16);
        }
        #endregion
    }
    class IsAny // service function
    {
        #region boolen
        public static bool IsMakeRead_as_nameCod(string so)
        {
            int il = so.Length;
            if (il > 4 && so.Contains(" (")) return true; else return false;
        }
        
        #endregion
    }
    class DATECOD
    {

        public static int yearCod_2_from_year2011(int iYear)
        {
            int y0 = 2010; int d = iYear - y0;
            int[] iy = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            return iy[d];
        }
        public static int year2011_from_yearCod_2(object  iYearCod)
        {
            return 2010 + Convert.ToInt32( iYearCod) - 1;
        }
        public static string get_Now_YearMonthDate_HourMinSec
        {
            get
            {
                DateTime dt = DateTime.Now;
                int y = dt.Year, m = dt.Month, d = dt.Day, h = dt.Hour, mi = dt.Minute, se = dt.Second;
                string s = string.Concat(y, shorts.nn(m), shorts.nn(d), "_", shorts.nn(h), shorts.nn(mi), shorts.nn(se));
                return s;
            }
        }
        public static int get_Now_YearMonthDate
        {
            get
            {
                DateTime dt = DateTime.Now; int y = dt.Year, m = dt.Month, d = dt.Day; return Convert.ToInt32(string.Concat(y, shorts.nn(m), shorts.nn(d)));
            }
        }
        
        #region date cod cod date
        public static string cod_to_date(object cod20100509)
        {
            string s = cod20100509.ToString(), ss;
            string y, mm, d;
            if (s.Length < 8) s = "20100101";
            y = s.Substring(0, 4); mm = s.Substring(4, 2); d = s.Substring(6, 2);
            ss = string.Concat(shorts.nn(d), ".", shorts.nn(mm), ".", y);
            return ss;
        }
        public static void m (object o) { System.Windows.Forms.MessageBox.Show(o.ToString()); }
        public static int date_to_cod_int(object date__01_01_2010)
        {
           // m(date__01_01_2010);
            DateTime dt = Convert.ToDateTime(date__01_01_2010);//functionService.DATECOD.cod_to_date(
            int y = dt.Year, mm = dt.Month, d = dt.Day;
            string ss = string.Concat(y, shorts.nn(mm), shorts.nn(d));
            return Convert.ToInt32(ss);
        }
        public static int date_month(object date__01_01_2010)
        {
            // m(date__01_01_2010);
            DateTime dt = Convert.ToDateTime(date__01_01_2010);//functionService.DATECOD.cod_to_date(
            return dt.Month;

        }
        public static int date_day(object date__01_01_2010)
        {
            // m(date__01_01_2010);
            DateTime dt = Convert.ToDateTime(date__01_01_2010);//functionService.DATECOD.cod_to_date(
            return dt.Day;
        }
        public static int date_year(object date__01_01_2010)
        {
            // m(date__01_01_2010);
            DateTime dt = Convert.ToDateTime(date__01_01_2010);//functionService.DATECOD.cod_to_date(
            return dt.Year;
        }
        //public static int date_to_cod_int(object date__01_01_2010)
        //{
        //    DateTime dt = Convert.ToDateTime(date__01_01_2010);
        //    int y = dt.Year, m = dt.Month, d = dt.Day;
        //    string ss = string.Concat(y, shorts.nn(m), shorts.nn(d));
        //    return Convert.ToInt32(ss);
        //}
        public static string date_to_cod_str(object date__01_01_2010)
        {
            DateTime dt = Convert.ToDateTime(date__01_01_2010);
            int y = dt.Year, m = dt.Month, d = dt.Day;
            string ss = string.Concat(y, shorts.nn(m), shorts.nn(d));
            return ss;
        }
        public static int codDATE_10913_from_DATETIME(object date__01_01_2010)
        {
           // functionService.shorts.m(111);
                int dc = date_to_cod_int(date__01_01_2010);
                int dcy = yearCod_2_from_year2011(Convert.ToInt32(dc.ToString().Substring(0, 4)));
                string dcd = dc.ToString().Substring(4, 4);
                string s = string.Concat(dcy, dcd);
                return Convert.ToInt32(s);
        }
        public static int codDATE_10913_from_codDATE_20100913(object coddate__20100913)
        {
            int dcy = yearCod_2_from_year2011(Convert.ToInt32(coddate__20100913.ToString().Substring(0, 4)));
            string dcd = coddate__20100913.ToString().Substring(4, 4);
            string s = string.Concat(dcy, dcd);
            return Convert.ToInt32(s);
        }
       
        
        #endregion
    }
    class tagWork
    {
        #region read tag
        public static string readTAG(string tago, int indexo)
        {
            string[] sa = tago.Split(';'); return sa[indexo];
        }
        public static string readTAG(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return sa[indexo];
        }
        public static int readTAGi(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return Convert.ToInt32(sa[indexo]);
        }
        public static string tv_tag(object oLEV_1o, object oCOD_2o, object oFO_3o)
        {
            return string.Concat(oLEV_1o, ";", oCOD_2o, ";", oFO_3o);
        }
        public static int[] readTAG_ToArrayInt(string tag)
        {
            string[] sa = tag.Split(';');  int[] ia = new int[sa.Length];
            for (int i = 0; i < ia.Length; i++){ ia[i] = Convert.ToInt32(sa[i]); }
            return ia;
        }
        public static string[] readTAG_ToArray(string tag)   {  return tag.Split(';');  }

        public static string putArray_ToTag(string[] anyArray)
        {
            string s = "";
            for (int i = 0; i < anyArray.Length;i++ ) {   if (i == 0) { s = anyArray[0]; } else {s = string.Concat(s,";", anyArray[i]); }    }
            return s;
        }
        public static string putArray_ToTag(int[] anyArray)
        {
            string s = "";
            for (int i = 0; i < anyArray.Length; i++) { if (i == 0) { s = anyArray[0].ToString(); } else { s = string.Concat(s, ";", anyArray[i]); } }
            return s;
        }
        #endregion
    }
    class nameCod // service function
    {
        #region name cod
        public static string name_cod(object name, object cod)
        {
            return string.Concat(name, " (", cod, ")");
        }
        public static string readName(string sText)
        {
            string s = sText, ss = "-"; int i; i = s.IndexOf("(") - 1;
            if (i > 0) { ss = s.Substring(0, i); } return ss;
        }
        public static string readCod(string sText)
        {
            string s = sText.Trim(), ss = "0"; int i, ii, il = s.Length; i = s.IndexOf("(") - 1; ii = il - (s.IndexOf("(") + 1) - 1;
            if (i > 0) { ss = s.Substring((s.IndexOf("(") + 1), ii); }
            return ss;
        }
        public static int readCod_int(string sText)
        {
            string s = sText.Trim(), ss = "0"; int i, ii, il = s.Length; i = s.IndexOf("(") - 1; ii = il - (s.IndexOf("(") + 1) - 1;
            if (i > 0) { ss = s.Substring((s.IndexOf("(") + 1), ii); }
            return Convert.ToInt32( ss);
        }

        #endregion
    }
    class nextCod // service function
    {
        #region next cod
        public int codNext(DataTable dt_oneClumnWithCod)
        {
            int i = 0, cod = 0, codNew = 1; string s = "", ss = "", s000 = "", s001 = "";
            foreach (DataRow r in dt_oneClumnWithCod.Rows)
            {
                i++;
                cod = Convert.ToInt32(r[0]);

                if (i != cod) { codNew = i; break; }
                else { codNew = i + 1; }
            }
            return codNew;
        }
        public static int codNext2(System.Data.DataTable dt_oneClumnWithCod)
        {
            int i = 1, cod = 0, codNew = 0;// string s = "", ss = "", s000 = "", s001 = "";
            for (int j = 0; j < dt_oneClumnWithCod.Rows.Count; j++)
            {
                cod = Convert.ToInt32(dt_oneClumnWithCod.Rows[j][0]);
                //shorts.m(shorts.a(cod, "=cod  i=", i));
                if (i != cod) { codNew = i; break; }
                else { codNew = i + 1; }
                i++;
            }
            //shorts.m(shorts.a(cod, "=cod  i=", i, "  codNew=", codNew));
            return codNew;
        }
        #endregion
    }
    public class prm // service function param
    {
        public System.Data.DataTable dtp;
        public string currentFileName;
        public int currentParamCOD;
        public prm()
        {
            currentFileName = "PLP"; currentParamCOD = 0;
            dtp = new System.Data.DataTable();
            readDTP();
        }
        public prm(string sfilenameWithoutFileExt)
            
        {
            dtp = new System.Data.DataTable();
            currentFileName = sfilenameWithoutFileExt;
            readDTP();
        }

        #region take put param
        public void readDTP(string sfilenameWithoutFileExto)
        {
            currentFileName = sfilenameWithoutFileExto;
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(currentFileName));
        }
        public void readDTP()
        {
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(currentFileName));
        }
        public void  readAnyXML_fromPLP_Dir_ToTableDTP(string fileNameWithoutExt)
        {
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(fileNameWithoutExt));
        }
        public string tk(int icod)
        {
            string s = "";
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { s = Convert.ToString(dtp.Rows[j][1]); break; }
            }
            return s;
        }
        public void sh(DataView dto)
        {
            GRID.FormGRID g = new GRID.FormGRID(dto); g.ShowDialog();
        }

        public  void sh(DataTable dto)
        {
            GRID.FormGRID g = new GRID.FormGRID(dto); g.ShowDialog();
        }
        public void save()
        {
            dtp.WriteXml(filePLP(currentFileName), System.Data.XmlWriteMode.WriteSchema);
        }
        public void put(int icod, object o)
        {
            bool b = false;
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { dtp.Rows[j][1] = o; dtp.AcceptChanges(); b = true; break; }
            }
            if (b) save();
        }
        public void putPrim(int icod, object o)
        {
            bool b = false;
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { dtp.Rows[j][2] = o; dtp.AcceptChanges(); b = true; break; }
            }
            if (b) save();
        }

        
        #endregion

        #region wwFile
        public void openFile(string fileNameo)
        {
            string stxt = fileNameo;//comboBox1.Text;
            lookDirAnOpenFile(stxt); dtp.WriteXml(filePLP(stxt), System.Data.XmlWriteMode.WriteSchema);
        }
        public void createFile(string fileNameo, System.Windows.Forms.ComboBox combb)
        {
            if (lookDir(fileNameo) == false) { HAND_createSimleTableParam(string.Concat("PLP\\", fileNameo)); readDir(combb); shorts.m("Файл создан"); }
            else { shorts.m(string.Concat("Файл с именем ", fileNameo, " уже существует")); }
        }
        public void editFile(string fileNameo)
        {
            string stxt = fileNameo;
            lookDirAnOpenFile(stxt); dtp.WriteXml(filePLP(stxt), System.Data.XmlWriteMode.WriteSchema);
        }
        public void deleteFile(string fileNameo, System.Windows.Forms.ComboBox combb)
        {
            string stxt = fileNameo;
            if (lookDir(stxt))
            {
                if (System.Windows.Forms.MessageBox.Show(string.Concat("Файл с именем ", stxt, " БУДЕТ удален"),
                    "Подтвердите Ваше намерение", System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    System.IO.File.Delete(filePLP(stxt)); readDir(combb); shorts.m(string.Concat("Файл с именем ", stxt, " УДАЛЕН"));
                }
                else { System.Windows.Forms.MessageBox.Show("Отмена"); }
            }
        }
        #endregion

        #region wwFile ser fun
        bool lookDir(string fileName)
        {
            bool IsHere = false;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("PLP");
            System.IO.FileInfo[] fai = di.GetFiles();
            foreach (System.IO.FileInfo fi in fai)
            {
                if (fileName == fi.Name.Replace(".xml", "")) { IsHere = true; break; }
            }
            return IsHere;
        }
        void lookDirAnOpenFile(string fileName)
        {

            if (lookDir(fileName))
            {
                if (dtp.Rows.Count > 0) { dtp.Clear(); }
                dtp.ReadXml(string.Concat("PLP\\", fileName, ".xml"));
                GRID.FormGRID g = new GRID.FormGRID(dtp);
                g.ShowDialog();
            }
        }
        public void readDir(System.Windows.Forms.ComboBox comboBox1)
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("PLP");
            System.IO.FileInfo[] fai = di.GetFiles();
            comboBox1.Items.Clear();
            foreach (System.IO.FileInfo fi in fai)
            {
                comboBox1.Items.Add(fi.Name.Replace(".xml", ""));
            }
        }
        void HAND_createSimleTableParam(string fileNameWithoutXML)
        {
            System.Data.DataTable t = new System.Data.DataTable("tp");
            t.Columns.Add("cod", typeof(int));
            t.Columns.Add("name", typeof(string));
            t.Columns.Add("prim", typeof(string));
            t.AcceptChanges();
            System.Data.DataRow r;
            r = t.NewRow();
            r[0] = 1; r[1] = "ol fio"; r[2] = "ol prim";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 2; r[1] = "ol fio2"; r[2] = "ol prim2";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 3; r[1] = "ol fio3"; r[2] = "ol prim3";
            t.Rows.Add(r);
            t.Columns[0].AutoIncrement = true;
            t.AcceptChanges();

            t.WriteXml(string.Concat(fileNameWithoutXML, ".xml"), System.Data.XmlWriteMode.WriteSchema);
        }
        string filePLP(string fileNameWithoutExt)
        {
            return string.Concat("PLP\\", fileNameWithoutExt, ".xml");
        }
        #endregion

    }
    class connDB
    {
        MySqlConnection conn;
        //DB_SQL.ConnectionDB_SQL cn;

        /// <summary>
        /// Соединение c БД MySQL
        /// </summary>
        /// <param name="server"></param> 
        /// <param name="db"></param>
        public void setConn(string server, string db)
        {
            conn = new MySqlConnection(string.Concat("server=", server, ";user id=muser; password=; database=", db, "; pooling=false; charset=utf8"));
        }
        public DataTable selectAny(string sSQL)
        {
            DataTable dt = new DataTable();

            try
            {
                MySqlCommand c = new MySqlCommand(sSQL);
                MySqlDataReader rd;
                c.Connection = conn;
                rd = c.ExecuteReader();

                dt.Load(rd, LoadOption.OverwriteChanges);
            }
            catch (Exception e) { shorts.m(e.Message); }
            return dt;

        }
        public void UpdateData(string strSQL_DML)
        {
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = strSQL_DML;
            cmd.ExecuteNonQuery();

        }
        public void ShowGrid()
        {
            conn.Open();
            DataTable dt = selectAny("SELECT 2+2;");
            conn.Close();
            userHK_grid.FormGRID f = new userHK_grid.FormGRID(dt);
            f.ShowDialog();
        }
        public void ShowGrid(string sqlo)
        {
            conn.Open();
            DataTable dt = selectAny(sqlo);
            conn.Close();
            userHK_grid.FormGRID f = new userHK_grid.FormGRID(dt);
            f.ShowDialog();
        }
        public void co()
        {
            conn.Open();
        }
        public void cc()
        {
            conn.Close();
        }
        public object selectScalar(string strSQL_SCALAR)
        {
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = strSQL_SCALAR;
            object o = cmd.ExecuteScalar();
            return o;
        }
    }
    class prm_grid // service function param
    {
        public System.Data.DataTable dtp;
        public string currentFileName;
        public int currentParamCOD;
        public prm_grid()
        {
            currentFileName = "PLP"; currentParamCOD = 0;
            dtp = new System.Data.DataTable();
            readDTP();
        }
        public prm_grid(string sfilenameWithoutFileExt)
        {
            dtp = new System.Data.DataTable();
            currentFileName = sfilenameWithoutFileExt;
            if (File.Exists("PLP\\" + currentFileName + ".xml"))
            {
                readDTP();
            }
            else
            {
                if (Directory.Exists("PLP"))
                {
                    createFile(currentFileName);
                    readDTP();
                }
                else
                {
                    Directory.CreateDirectory("PLP");
                    createFile(currentFileName);
                    readDTP();
                }
            }
        }

        #region take put param
        public void readDTP(string sfilenameWithoutFileExto)
        {
            currentFileName = sfilenameWithoutFileExto;
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(currentFileName));
        }
        public void readDTP()
        {
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(currentFileName));
        }
        public void readAnyXML_fromPLP_Dir_ToTableDTP(string fileNameWithoutExt)
        {
            dtp.Rows.Clear();
            dtp.ReadXml(filePLP(fileNameWithoutExt));
        }

        public string tk(int icod)
        {
            string s = "";
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { s = Convert.ToString(dtp.Rows[j][1]); break; }
            }
            return s;
        }

        public void sh(DataTable dto)
        {
            userHK_grid.FormGRID g = new userHK_grid.FormGRID(dto); g.ShowDialog();
        }
        public void save()
        {
            dtp.WriteXml(filePLP(currentFileName), System.Data.XmlWriteMode.WriteSchema);
        }
        public void put(int icod, object o)
        {
            bool b = false;
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { dtp.Rows[j][1] = o; dtp.AcceptChanges(); b = true; break; }
            }
            if (b) save();
        }
        public void putPrim(int icod, object o)
        {
            bool b = false;
            for (int j = 0; j < dtp.Rows.Count; j++)
            {
                if (icod == Convert.ToInt32(dtp.Rows[j][0])) { dtp.Rows[j][2] = o; dtp.AcceptChanges(); b = true; break; }
            }
            if (b) save();
        }


        #endregion

        #region wwFile
        public void openFile(string fileNameo)
        {
            string stxt = fileNameo;//comboBox1.Text;
            lookDirAnOpenFile(stxt); dtp.WriteXml(filePLP(stxt), System.Data.XmlWriteMode.WriteSchema);
        }
        public void createFile(string fileNameo)
        {
            HAND_createSimleTableParam(string.Concat("PLP\\", fileNameo));

        }
        public void editFile(string fileNameo)
        {
            string stxt = fileNameo;
            lookDirAnOpenFile(stxt); dtp.WriteXml(filePLP(stxt), System.Data.XmlWriteMode.WriteSchema);
        }
        public void deleteFile(string fileNameo, System.Windows.Forms.ComboBox combb)
        {
            string stxt = fileNameo;
            if (lookDir(stxt))
            {
                if (System.Windows.Forms.MessageBox.Show(string.Concat("Файл с именем ", stxt, " БУДЕТ удален"),
                    "Подтвердите Ваше намерение", System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    System.IO.File.Delete(filePLP(stxt)); readDir(combb); shorts.m(string.Concat("Файл с именем ", stxt, " УДАЛЕН"));
                }
                else { System.Windows.Forms.MessageBox.Show("Отмена"); }
            }
        }
        #endregion

        #region wwFile ser fun
        bool lookDir(string fileName)
        {
            bool IsHere = false;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("PLP");
            System.IO.FileInfo[] fai = di.GetFiles();
            foreach (System.IO.FileInfo fi in fai)
            {
                if (fileName == fi.Name.Replace(".xml", "")) { IsHere = true; break; }
            }
            return IsHere;
        }
        void lookDirAnOpenFile(string fileName)
        {

            if (lookDir(fileName))
            {
                if (dtp.Rows.Count > 0) { dtp.Clear(); }
                dtp.ReadXml(string.Concat("PLP\\", fileName, ".xml"));
                userHK_grid.FormGRID g = new userHK_grid.FormGRID(dtp);
                g.ShowDialog();
            }
        }
        public void readDir(System.Windows.Forms.ComboBox comboBox1)
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("PLP");
            System.IO.FileInfo[] fai = di.GetFiles();
            comboBox1.Items.Clear();
            foreach (System.IO.FileInfo fi in fai)
            {
                comboBox1.Items.Add(fi.Name.Replace(".xml", ""));
            }
        }
        void HAND_createSimleTableParam(string fileNameWithoutXML)
        {
            System.Data.DataTable t = new System.Data.DataTable("tp");
            t.Columns.Add("cod", typeof(int));
            t.Columns.Add("name", typeof(string));
            t.Columns.Add("prim", typeof(string));
            t.AcceptChanges();
            System.Data.DataRow r;

            r = t.NewRow();
            r[0] = 1; r[1] = "3"; r[2] = "Количество колонок";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 2; r[1] = "100;100;100"; r[2] = "Ширины столбцов";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 3; r[1] = "id;name;prim"; r[2] = "Подписи столбцов";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 4; r[1] = "Arial"; r[2] = "Шрифт";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 5; r[1] = "10"; r[2] = "Размер шрифта";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 6; r[1] = "Regular"; r[2] = "Стиль шрифта";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 7; r[1] = "System.String;System.String;System.String"; r[2] = "Типы полей";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 8; r[1] = "SELECT * FROM anyTable LIMIT anyNum"; r[2] = "Запрос";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 9; r[1] = "01.12.2010"; r[2] = "Период с";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 10; r[1] = "01.01.2011"; r[2] = "Период по";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 11; r[1] = "name_col"; r[2] = "Имя выбранной для поиска колонки";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 12; r[1] = "SELECT '0' AS id,'0' AS name,'0' AS prim;"; r[2] = "SQL_1";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 13; r[1] = "2;SQL_2;"; r[2] = "SQL_2";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 14; r[1] = "3;SQL_3;"; r[2] = "SQL_3";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 15; r[1] = "4;SQL_4;"; r[2] = "SQL_4";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 16; r[1] = "5;SQL_5;"; r[2] = "SQL_5";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 17; r[1] = "6;SQL_6;"; r[2] = "SQL_6";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 18; r[1] = "7;SQL_7;"; r[2] = "SQL_7";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 19; r[1] = "8;SQL_8;"; r[2] = "SQL_8";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 20; r[1] = "9;SQL_9;"; r[2] = "SQL_9";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 21; r[1] = "10;SQL_10;"; r[2] = "SQL_10";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 22; r[1] = "1;FIND_1;"; r[2] = "FIND_1";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 23; r[1] = "2;FIND_2;"; r[2] = "FIND_2";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 24; r[1] = "3;FIND_3;"; r[2] = "FIND_3";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 25; r[1] = "4;FIND_4;"; r[2] = "FIND_4";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 26; r[1] = "5;FIND_5;"; r[2] = "FIND_5";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 27; r[1] = "6;FIND_6;"; r[2] = "FIND_6";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 28; r[1] = "7;FIND_7;"; r[2] = "FIND_7";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 29; r[1] = "8;FIND_8;"; r[2] = "FIND_8";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 30; r[1] = "9;FIND_9;"; r[2] = "FIND_9";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 31; r[1] = "10;FIND_10;"; r[2] = "FIND_10";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 32; r[1] = "Опасный заголовок"; r[2] = "Заголовок таблицы";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 33; r[1] = "localhost"; r[2] = "Выбранный сервер";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 34; r[1] = "lists"; r[2] = "shema";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 35; r[1] = "ITOGO"; r[2] = "Нижний колонтитул";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 36; r[1] = "false"; r[2] = "Ориентация страницы LANDSCAPE";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 37; r[1] = "id"; r[2] = "Ключевые поля";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 38; r[1] = "tableName"; r[2] = "Имя отчета";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 39; r[1] = "look_cell"; r[2] = "faed";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 40; r[1] = "1"; r[2] = "rowCount";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 41; r[1] = "0;0;0"; r[2] = "Редактирование колонок входящий";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 42; r[1] = "editDisable;editDisable;editDisable"; r[2] = "Редактирование колонок типы";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 43; r[1] = "DISABLE"; r[2] = "Имя таблицы";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 44; r[1] = "1"; r[2] = "Текущая ячейка X";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 45; r[1] = "1"; r[2] = "Текущая ячейка Y";
            t.Rows.Add(r);


            t.Columns[0].AutoIncrement = true;
            t.AcceptChanges();

            t.WriteXml(string.Concat(fileNameWithoutXML, ".xml"), System.Data.XmlWriteMode.WriteSchema);
        }
        string filePLP(string fileNameWithoutExt)
        {
            return string.Concat("PLP\\", fileNameWithoutExt, ".xml");
        }
        #endregion



    }
    public class prm_report : prm
       {
           public prm_report(string fileNameParam)
           {
            
               dtp = new System.Data.DataTable();
               currentFileName = fileNameParam;
               if (File.Exists("PLP\\" + fileNameParam + ".xml"))
               {
                   readDTP();
               }
               else
               {
                   if (Directory.Exists("PLP"))
                   {
                       createFile(fileNameParam);
                       readDTP();
                   }
                   else
                   {
                       Directory.CreateDirectory("PLP");
                       createFile(fileNameParam);
                       readDTP();
                   }
               }
           }
           public void createFile(string fileNameo)
           {
               HAND_createSimleTableParam(string.Concat("PLP\\", fileNameo));

           }
           public void HAND_createSimleTableParam(string fileNameWithoutXML)
           {
               System.Data.DataTable t = new System.Data.DataTable("tp");
               t.Columns.Add("cod", typeof(int));
               t.Columns.Add("name", typeof(string));
               t.Columns.Add("prim", typeof(string));
               t.AcceptChanges();
               System.Data.DataRow r;

               r = t.NewRow();
               r[0] = 1; r[1] = "200;200;200"; r[2] = "Ширины колонок";
               t.Rows.Add(r);
               r = t.NewRow();
               r[0] = 2; r[1] = "кол1;кол2;кол3"; r[2] = "Наименования колонок";
               t.Rows.Add(r);

               t.Columns[0].AutoIncrement = true;
               t.AcceptChanges();

               t.WriteXml(string.Concat(fileNameWithoutXML, ".xml"), System.Data.XmlWriteMode.WriteSchema);
           }
       }
}

